package org.thirdHero.db.service;

import com.github.pagehelper.PageHelper;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.thirdHero.core.constant.CommonConstant;
import org.thirdHero.core.util.StringUtil;
import org.thirdHero.db.dao.SysCodeMapper;
import org.thirdHero.db.domain.SysCode;
import org.thirdHero.db.domain.SysCodeExample;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class SysCodeService {
    private final static Logger logger = LoggerFactory.getLogger(SysCodeService.class);

    @Resource
    private SysCodeMapper sysCodeMapper;

    /**
     * 分页查询
     * @param sysCode 查询对象
     * @param page 当前页
     * @param limit 每页显示条数
     * @param sort 排序字段
     * @param order 升序/降序 desc/asc
     * @return
     */
    public List<SysCode> pageList(SysCode sysCode, Integer page, Integer limit, String sort, String order) {
        SysCodeExample sysCodeExample = commonSelective(sysCode);
        sysCodeExample.setOrderByClause(sort);
        sysCodeExample.orderBy(order);
        PageHelper.startPage(page,limit);
        return this.sysCodeMapper.selectByExample(sysCodeExample);
    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    public SysCode queryById(String id){
        return this.sysCodeMapper.selectByPrimaryKey(id);
    }
    /**
     * 更新信息
     * @param sysCode
     * @return
     */
    public int updateByPrimaryKeySelective(SysCode sysCode) {
        if(Strings.isNullOrEmpty(sysCode.getId())){
            return 0;
        }
        return sysCodeMapper.updateByPrimaryKeySelective(sysCode);
    }

    /**
     * 根据主键更新所有内容
     * @param sysCode
     * @return
     */
    public int updateByPrimaryKey(SysCode sysCode){
        if(Strings.isNullOrEmpty(sysCode.getId())){
            return 0;
        }
        return sysCodeMapper.updateByPrimaryKey(sysCode);
    }
    /**
     * 伪删除
     * @param  sysCode
     * @return
     */
    public int pseudoDel(SysCode sysCode){
        if(Strings.isNullOrEmpty(sysCode.getId())){
            return 0;
        }
        SysCode preDelParam = new SysCode();
        preDelParam.setId(sysCode.getId());
        preDelParam.setDeleted(CommonConstant.IS_DELETE);
        preDelParam.setModifyDate(LocalDateTime.now());
        return sysCodeMapper.updateByPrimaryKeySelective(preDelParam);
    }
    /**
     * 根据id查询信息
     * @param sysCode
     * @return
     */
    public int delete(SysCode sysCode) {
        if(Strings.isNullOrEmpty(sysCode.getId())){
            return 0;
        }
        return sysCodeMapper.deleteByPrimaryKey(sysCode.getId());
    }
    /**
     * 保存信息
     * @param sysCode
     * @return
     */
    public int saveSysCode(SysCode sysCode) {
        sysCode.setCreateDate(LocalDateTime.now());
        sysCode.setModifyDate(LocalDateTime.now());
        sysCode.setDeleted(CommonConstant.NOT_DELETE);
        sysCode.setId(StringUtil.getUUID());
        return sysCodeMapper.insert(sysCode);
    }

    /**
     * 根据example统计个数
     * @param sysCode
     * @return
     */
    public long countByExample(SysCode sysCode){
        SysCodeExample sysCodeExample = commonSelective(sysCode);
        return sysCodeMapper.countByExample(sysCodeExample);
    }

    /**
     * 根据example删除内容
     * @param sysCode
     * @return
     */
    public int deleteByExample(SysCode sysCode){
        SysCodeExample sysCodeExample = commonSelective(sysCode);
        return sysCodeMapper.deleteByExample(sysCodeExample);
    }

    /**
     * 根据example查询单个数据
     * @param sysCode
     * @return
     */
    public SysCode selectOneByExample(SysCode sysCode){
        SysCodeExample sysCodeExample = commonSelective(sysCode);
        return sysCodeMapper.selectOneByExample(sysCodeExample);
    }

    /**
     * 根据example查询数据
     * @param sysCode
     * @return
     */
    public List<SysCode> selectByExample(SysCode sysCode){
        SysCodeExample sysCodeExample = commonSelective(sysCode);
        return sysCodeMapper.selectByExample(sysCodeExample);

    }


    /**
     * 通用查询
     * @param sysCode
     * @return
     */
    private SysCodeExample commonSelective(SysCode sysCode) {
        SysCodeExample example = new SysCodeExample();
        SysCodeExample.Criteria criteria = example.createCriteria();
        if(!Strings.isNullOrEmpty(sysCode.getParentCode())){
            criteria.andParentCodeEqualTo(sysCode.getParentCode());
        }
        if(!Strings.isNullOrEmpty(sysCode.getCode())){
            criteria.andCodeEqualTo(sysCode.getCode());
        }
        if(!Strings.isNullOrEmpty(sysCode.getCodeName())){
            criteria.andCodeNameLike("%"+sysCode.getCodeName()+"%");
        }
        if(!Strings.isNullOrEmpty(sysCode.getGroupCode())){
            criteria.andGroupCodeLike("%"+sysCode.getGroupCode()+"%");
        }
        if(sysCode.getCreateDate()!=null){
            criteria.andCreateDateGreaterThanOrEqualTo(sysCode.getCreateDate());
        }
        return example;
    }
   /**
     * 查询所有信息
     * @return
     */
    public List<SysCode> queryAll( ) {
        SysCodeExample sysCodeExample = new SysCodeExample();
        return this.sysCodeMapper.selectByExample(sysCodeExample);
    }
}
